<?php
namespace app\common\model\driver;

use app\common\model\user\User;
use think\facade\Request;
use app\common\model\BaseModel;

class MyDriver extends BaseModel
{
    protected $name = 'my_driver';
    protected $pk = 'id';

    public function user()
    {
        return $this->hasOne(User::Class)->field('mobile');
    }

    public function get_list($param)
    {
        $where = array();
        if($param['user_id']){
            $where[] = ['user_id','=',$param['user_id']];
        }
        $where[] = ['is_delete','=',0];
        $carlist = $this->alias('d')
            ->field('d.*,da.driver_age as age_name,dt.type_name')
            ->join("jjjshop_driver_age da","d.driver_age=da.id","left")
            ->join("jjjshop_driver_type dt","d.driver_type=dt.type_id","left")
            ->where($where)
            ->order('create_time desc')->paginate($param,false);

        return $carlist;
    }

    public function add($data)
    {
        unset($data['app_id']);
        if(isset($data['id'])){
            $data['status'] = 0;
            $res = $this->where(array('id'=>$data['id']))->update($data);
        }else{
            $res = $this->save($data);
        }
        return $res;
    }

    public function get_detail($id)
    {
        $info = $this->alias('d')
            ->field('d.*,da.driver_age as age_name,dt.type_name')
            ->join("jjjshop_driver_age da","d.driver_age=da.id","left")
            ->join("jjjshop_driver_type dt","d.driver_type=dt.type_id","left")
            ->where(array('id'=>$id))->find();
        return $info;
    }

    public function del($id)
    {
        $res = $this->where(array('id'=>$id))->update(array('is_delete'=>1));
        return $res;
    }
}